package com.fly.r2dbc.basics.service.impl;

import java.time.Duration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.fly.r2dbc.basics.dao.CustomerRepository;
import com.fly.r2dbc.basics.entity.Customer;
import com.fly.r2dbc.basics.service.CustomerService;

import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;

/**
 * 
 * CustomerService 接口实现类
 * 
 * @author 00fly
 * @version [版本号, 2023-05-18]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service
public class CustomerServiceImpl implements CustomerService
{
    @Autowired
    CustomerRepository customerDAO;
    
    @Override
    public void insert(Customer customer)
    {
        customerDAO.save(customer);
    }
    
    @Override
    public void deleteById(Long id)
    {
        customerDAO.deleteById(id);
    }
    
    @Override
    public void update(Customer customer)
    {
        customerDAO.save(customer);
    }
    
    @Override
    public void saveOrUpdate(Customer customer)
    {
        if (customer.getId() == null)
        {
            customerDAO.save(customer);
        }
        else
        {
            customerDAO.save(customer);
        }
    }
    
    @Override
    public Customer queryById(Long id)
    {
        return customerDAO.findById(id).block(Duration.ofSeconds(3));
    }
    
    @Override
    public Flux<Customer> findAll()
    {
        return customerDAO.findAll();
    }
    
    /**
     * 
     * @param pageable
     * @return
     */
    @Override
    public Flux<Customer> findBy(Pageable pageable)
    {
        return customerDAO.findBy(pageable);
    }
    
    @Override
    public long count()
    {
        return customerDAO.count().block(Duration.ofSeconds(3));
    }
}
